/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_together.js                                 
*  Description  : sky72 회원연동 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-18  김효순    Created.
*  2012-04-18  김효순    Complete.
*******************************************************************************/

jQuery(function($){
	// 아이디
	function clsId()
	{
		this._HTMLDom = null;
		this._OBJERR = {
			"_NOVALUE": "아이디를 입력해 주십시요."
		}
		this.errMsg = null;
		this._init();
	}
	
	clsId.prototype = {
		_init: function(){
			this._HTMLDom = {
				"$layer": $("div.pw_area input:text")
			}
			
			this._HTMLDom["$layer"].attr("readonly", "readonly");
		}
		, setValue: function(val){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.val(val);
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.val();
		}
		, isValidate: function(){
			var $layer = this._HTMLDom["$layer"];
			var val = $layer.val();
			
			if(!val){
				this.errMsg = this._OBJERR["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
	}
	
	// 비밀번호
	function clsPw()
	{
		this._HTMLDom = null;
		this._OBJERR = {
			"_NOVALUE": "비밀번호를 입력해 주십시요."
		}
		this.errMsg = null;
		this._init();
	}
	
	clsPw.prototype = {
		_init: function(){
			this._HTMLDom = {
				"$layer": $("div.pw_area input:password")
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.val();
		}
		, isValidate: function(){
			var $layer = this._HTMLDom["$layer"];
			var val = $layer.val();
			
			if(!val){
				this.errMsg = this._OBJERR["_NOVALUE"];
				return false;
			}
			
			return true;
		}
		, getError: function(){
			return this.errMsg;
		}
	}
	
	function clsComm()
	{
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		, validSubmit: function(arg){
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "getSky72UserData";
			var options = {
				"userId": arg["아이디"]
				, "pw": arg["비밀번호"]
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					
					res["결과값"] = {
						"아이디": arg["아이디"]
						, "이메일": _util.nvl(obj["email"])
						, "휴대폰": _util.nvl(obj["phonenum"])
						, "성별코드": _util.nvl(obj["sex"])
						, "이름": _util.nvl(obj["userName"])
						, "통신사": _util.nvl(obj["telInc"])
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	function clsControl()
	{
		this.objComm = null;
		this.objId = null;
		this.objPw = null;
		this.openerControl = null;
		
		this.q_id = null;
		this._permitStart = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objId = new clsId();
			this.objPw = new clsPw();
			
			this._bind();
			this._beforeStart();
		}
		, _preload: function(){
			_gf.spinStart();
			
			if(opener && opener.document && opener.document.objControl) this.openerControl = opener.document.objControl;
			
			this.q_id = _util.getParam("q_id");
			
			if(!this.openerControl) self.close();
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass.start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 전송하기
			this.objPw.$getLayer().on("keydown", null, function(){
				var e = e || event;
				
				if(e.keyCode == 13){
					thisClass.validSubmit();
				}
			});
			
			$(document).on("click", "img[alt='연동하기']", function(){
				thisClass.validSubmit();
			});
			
			// 닫기 버튼
			$("p.pop_close img").css("cursor", "pointer").click(function(){
				self.close();
			});
		}
		, start: function(){
			this.objId.setValue(this.q_id);
			
			document.objUtil.setWindowSize();
		}
		// 전송완료
		, finishSubmit: function(ret){
			_gf.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.openerControl.setSky72(window, ret["결과값"]);
			self.close();
		}
		// 전송요청
		, validSubmit: function(){
			var thisClass = this;
			
			// id체크
			if(!this.objId.isValidate()){
				alert(this.objId.getError());
				return;
			// 비밀번호 체크
			}else if(!this.objPw.isValidate()){
				alert(this.objPw.getError());
				return;
			}
			
			_gf.spinStart("sending...");
			thisClass.objComm.validSubmit({
				"아이디": this.objId.getValue()
				, "비밀번호": this.objPw.getValue()
				, "콜백": function(ret){
					thisClass.finishSubmit(ret);
				}
			});
		}
	}
	
	document.objControl = new clsControl();
});